package com.sleepcamel.resteasyConfigurator.view.detail;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.wb.swt.SWTResourceManager;

import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.resteasyConfigurator.components.MimeTypeListChooser;

public class MappingDetailView extends DetailView<Mapping> {

	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private Text txtNewText_1;
	private Text txtNewText_3;
	private Button btnCheckButton;
	private MimeTypeListChooser producesChooser;
	private MimeTypeListChooser consumesChooser;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MappingDetailView(Composite parent, int style) {
		super(parent, SWT.NONE);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = formToolkit.createComposite(this, SWT.NONE);
		{
			TableWrapLayout twl_composite = new TableWrapLayout();
			twl_composite.numColumns = 4;
			composite.setLayout(twl_composite);
		}
		formToolkit.paintBordersFor(composite);
		
		Label lblNewLabel = formToolkit.createLabel(composite, "Source name", SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		txtNewText_1 = formToolkit.createText(composite, "", SWT.NONE);
		txtNewText_1.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 3));
		txtNewText_1.setMessage("Source name");
		
		Label lblNewLabel_2 = formToolkit.createLabel(composite, "Path", SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		txtNewText_3 = formToolkit.createText(composite, "", SWT.NONE);
		txtNewText_3.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 3));
		txtNewText_3.setMessage("Path");
		
		Label lblEncoded = new Label(composite, SWT.NONE);
		lblEncoded.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		formToolkit.adapt(lblEncoded, true, true);
		lblEncoded.setText("Encoded");
		
		btnCheckButton = new Button(composite, SWT.CHECK);
		btnCheckButton.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 3));
		formToolkit.adapt(btnCheckButton, true, true);
		
		Label lblProduces = formToolkit.createLabel(composite, "Produces", SWT.NONE);
		lblProduces.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		producesChooser = new MimeTypeListChooser(composite, SWT.NONE);
		producesChooser.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		
		Label lblConsumes = formToolkit.createLabel(composite, "Consumes", SWT.NONE);
		lblConsumes.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		consumesChooser = new MimeTypeListChooser(composite, SWT.NONE);
		consumesChooser.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = super.initDataBindings();
		//
		IObservableValue txtNewText_1ObserveTextObserveWidget = SWTObservables.observeText(txtNewText_1, SWT.Modify);
		IObservableValue mappingNameObserveValue = BeansObservables.observeValue(modelData, "name");
		bindingContext.bindValue(txtNewText_1ObserveTextObserveWidget, mappingNameObserveValue, null, null);
		//
		IObservableValue txtNewText_3ObserveTextObserveWidget = SWTObservables.observeText(txtNewText_3, SWT.Modify);
		IObservableValue mappingPathObserveValue = BeansObservables.observeValue(modelData.getPath(), "value");
		bindingContext.bindValue(txtNewText_3ObserveTextObserveWidget, mappingPathObserveValue, null, null);
		//
		IObservableValue encodedValue = SWTObservables.observeSelection(btnCheckButton);
		IObservableValue mappingEncodedObserveValue = BeansObservables.observeValue(modelData, "encodable");
		bindingContext.bindValue(encodedValue, mappingEncodedObserveValue, null, null);
		
		producesChooser.setSelectedChoices(modelData.getProduces());
		consumesChooser.setSelectedChoices(modelData.getConsumes());
		return bindingContext;
	}
}
